package com.cyl.manager.pms.domain.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cyl.manager.pms.domain.entity.ProductCategory;
import com.cyl.manager.pms.domain.service.ProductCategoryDomainService;
import com.cyl.manager.pms.mapper.ProductCategoryMapper;
import com.cyl.manager.pms.req.ProductCategoryQueryReq;
import com.sincere.common.utils.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author: lsh
 * @create: 2025-01-22 14:50
 **/
@Service
public class ProductCategoryDomainServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements ProductCategoryDomainService {

    private final ProductCategoryMapper productCategoryMapper;

    public ProductCategoryDomainServiceImpl(ProductCategoryMapper productCategoryMapper) {
        this.productCategoryMapper = productCategoryMapper;
    }

    @Override
    public List<ProductCategory> listByPage(ProductCategoryQueryReq query, Pageable page) {
        QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(query.getNameLike()),ProductCategory.COL_NAME, query.getNameLike());
        queryWrapper.eq(Objects.nonNull(query.getSort()),ProductCategory.COL_SORT, query.getSort());
        queryWrapper.eq(Objects.nonNull(query.getParentId()),ProductCategory.COL_PARENT_ID, query.getParentId());
        queryWrapper.eq(Objects.nonNull(query.getShowStatus()),ProductCategory.COL_SHOW_STATUS, query.getShowStatus());
        queryWrapper.eq(Objects.nonNull(query.getLevel()),ProductCategory.COL_LEVEL, query.getLevel());
        queryWrapper.orderByAsc(ProductCategory.COL_SORT);
        return productCategoryMapper.selectList(queryWrapper);
    }

    @Override
    public List<ProductCategory> listAll() {
        QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByAsc(ProductCategory.COL_SORT);
        return productCategoryMapper.selectList(queryWrapper);
    }

    @Override
    public List<ProductCategory> listByParentId(Long parentId) {
      //根据parentId 获取分类列表
        QueryWrapper<ProductCategory> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(ProductCategory.COL_PARENT_ID, parentId);
        queryWrapper.eq(ProductCategory.COL_SHOW_STATUS, 1);
        queryWrapper.orderByAsc(ProductCategory.COL_SORT);
        return productCategoryMapper.selectList(queryWrapper);
    }
}
